<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ActiveJoinModel extends AbstractModel
{
    protected $tableName = 't_active_join';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception|Throwable
     */
    public function getActive($where)
    {
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addActive($where)
    {
        $log = $this->getActive(['uid' => $where['uid'], 'gid' => $where['gid']]);
        if ($log) {
            return true;
        }
        try {
            $model = new ActiveModel();
            $result = $model->getActive(['id' => $where['gid']]);
            if (!$result) {
                return false;
            }
            // 开启事务
            \EasySwoole\ORM\DbManager::getInstance()->startTransaction();

            $this->data($where, false);
            $res = $this->save();

            $upRes = $model->update(['see' => $result->see + 1, 'join' => $result->join + 1], ['id' => $where['gid']]);

            // 提交事务
            \EasySwoole\ORM\DbManager::getInstance()->commit();

            return $res;
        } catch (Throwable $e) {
            // 回滚事务
            \EasySwoole\ORM\DbManager::getInstance()->rollback();
            return false;
        }
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('活动参与表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment("用户ID");
        $table->colInt('gid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment("活动id");
        $table->colInt('num', 25)->setDefaultValue(0)->setColumnComment("参与人数");
        $table->colInt('code', 25)->setDefaultValue(0)->setColumnComment("识别码");
        $table->colInt('status', 2)->setDefaultValue(0)->setColumnComment("参与状态：0.待确认， 1.确认");
        $table->colInt('complete', 2)->setDefaultValue(0)->setColumnComment("核销状态：0.待确认， 1.确认");
        $table->colVarChar('name', 64)->setDefaultValue('')->setColumnComment('联系人姓名');
        $table->colVarChar('tel', 64)->setDefaultValue('')->setColumnComment('联系人电弧');

        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('gid_index', 'gid');
        $table->indexNormal('num_index', 'num');

        return $table;
    }
}